import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _class, _class2;

import React from 'react';
import { Form, Input, Modal, Tooltip, Icon } from 'antd';
import { Bind } from 'lodash-decorators';
import uuid from 'uuid/v4';
import EditTable from 'components/EditTable';
import { Button as ButtonPermission } from 'components/Permission';
import intl from 'utils/intl';
import { getEditTableData } from 'utils/utils';
import { operatorRender } from 'utils/renderer';
var AppSourceForm = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(AppSourceForm, _React$PureComponent);

  var _super = _createSuper(AppSourceForm);

  function AppSourceForm(props) {
    var _this;

    _classCallCheck(this, AppSourceForm);

    _this = _super.call(this, props);
    _this.state = {
      selectedRows: [],
      selectedRowKeys: []
    };
    return _this;
  }

  _createClass(AppSourceForm, [{
    key: "handleOk",
    value: function handleOk() {
      var _this$props = this.props,
          _this$props$onOk = _this$props.onOk,
          onOk = _this$props$onOk === void 0 ? function (e) {
        return e;
      } : _this$props$onOk,
          dataSource = _this$props.dataSource;
      var params = getEditTableData(dataSource, ['emailFilterId']);

      if (Array.isArray(params) && params.length > 0) {
        onOk(params);
      }
    }
  }, {
    key: "handleSelectChange",
    value: function handleSelectChange(selectedRowKeys, selectedRows) {
      this.setState({
        selectedRows: selectedRows,
        selectedRowKeys: selectedRowKeys
      });
    }
  }, {
    key: "handleDelete",
    value: function handleDelete() {
      var _this$props$onDelete = this.props.onDelete,
          onDelete = _this$props$onDelete === void 0 ? function (e) {
        return e;
      } : _this$props$onDelete;
      var _this$state = this.state,
          selectedRows = _this$state.selectedRows,
          selectedRowKeys = _this$state.selectedRowKeys;
      onDelete(selectedRows, selectedRowKeys);
    }
  }, {
    key: "handleCreate",
    value: function handleCreate() {
      var _this$props2 = this.props,
          _this$props2$onCreate = _this$props2.onCreate,
          onCreate = _this$props2$onCreate === void 0 ? function (e) {
        return e;
      } : _this$props2$onCreate,
          currentFilter = _this$props2.currentFilter;
      onCreate({
        _status: 'create',
        emailFilterId: uuid(),
        serverId: currentFilter.serverId,
        address: ''
      });
    }
  }, {
    key: "handlePagination",
    value: function handlePagination(pagination) {
      var _this$props$onSearch = this.props.onSearch,
          onSearch = _this$props$onSearch === void 0 ? function (e) {
        return e;
      } : _this$props$onSearch;
      onSearch({
        page: pagination
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          _this$props3$visible = _this$props3.visible,
          visible = _this$props3$visible === void 0 ? false : _this$props3$visible,
          _this$props3$loading = _this$props3.loading,
          loading = _this$props3$loading === void 0 ? false : _this$props3$loading,
          _this$props3$deleteLo = _this$props3.deleteLoading,
          deleteLoading = _this$props3$deleteLo === void 0 ? false : _this$props3$deleteLo,
          _this$props3$fetchLoa = _this$props3.fetchLoading,
          fetchLoading = _this$props3$fetchLoa === void 0 ? false : _this$props3$fetchLoa,
          _this$props3$onCancel = _this$props3.onCancel,
          onCancel = _this$props3$onCancel === void 0 ? function (e) {
        return e;
      } : _this$props3$onCancel,
          _this$props3$onEdit = _this$props3.onEdit,
          onEdit = _this$props3$onEdit === void 0 ? function (e) {
        return e;
      } : _this$props3$onEdit,
          _this$props3$dataSour = _this$props3.dataSource,
          dataSource = _this$props3$dataSour === void 0 ? [] : _this$props3$dataSour,
          _this$props3$paginati = _this$props3.pagination,
          pagination = _this$props3$paginati === void 0 ? {} : _this$props3$paginati,
          path = _this$props3.path;
      var _this$state$selectedR = this.state.selectedRows,
          selectedRows = _this$state$selectedR === void 0 ? [] : _this$state$selectedR;
      var columns = [{
        title: /*#__PURE__*/React.createElement(React.Fragment, null, intl.get('cmsg.email.model.email.name').d('邮箱'), /*#__PURE__*/React.createElement(Tooltip, {
          title: intl.get('cmsg.email.model.email.userName.filterType').d('邮箱账户或邮箱服务器')
        }, /*#__PURE__*/React.createElement(Icon, {
          type: "question-circle-o",
          style: {
            marginLeft: 5
          }
        }))),
        dataIndex: 'address',
        render: function render(val, record) {
          if (record._status === 'create' || record._status === 'update') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(Form.Item, null, getFieldDecorator('address', {
              initialValue: val
            })( /*#__PURE__*/React.createElement(Input, {
              trim: true
            })));
          } else {
            return val;
          }
        }
      }, {
        title: intl.get('cpaas.common.button.action').d('操作'),
        width: 80,
        render: function render(val, record) {
          var operators = [];

          if (record._status === 'update') {
            operators.push({
              key: 'cancel',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.cancelEdit"),
                  type: 'button',
                  meaning: '邮箱账户-取消编辑'
                }],
                onClick: function onClick() {
                  return onEdit(record, false);
                }
              }, intl.get('cpaas.common.button.cancel').d('取消')),
              len: 2,
              title: intl.get('cpaas.common.button.cancel').d('取消')
            });
          } else if (record._status !== 'create') {
            operators.push({
              key: 'edit',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.editFilter"),
                  type: 'button',
                  meaning: '邮箱账户-编辑黑白名单'
                }],
                onClick: function onClick() {
                  return onEdit(record, true);
                }
              }, intl.get('cpaas.common.button.edit').d('编辑')),
              len: 2,
              title: intl.get('cpaas.common.button.edit').d('编辑')
            });
          }

          return operatorRender(operators);
        }
      }];
      var rowSelection = {
        onChange: this.handleSelectChange
      };
      return /*#__PURE__*/React.createElement(Modal, {
        destroyOnClose: true,
        wrapClassName: "ant-modal-sidebar-right",
        transitionName: "move-right",
        title: intl.get('cmsg.email.view.message.title.filter').d('编辑黑白名单'),
        width: "620px",
        visible: visible,
        confirmLoading: loading,
        onCancel: onCancel,
        onOk: this.handleOk
      }, /*#__PURE__*/React.createElement("div", {
        style: {
          display: 'flex',
          justifyContent: 'flex-end'
        },
        className: "table-list-operator"
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.createFilter"),
          type: 'button',
          meaning: '邮箱账户-新建黑白名单'
        }],
        onClick: this.handleCreate
      }, intl.get('cpaas.common.status.create').d('新建')), /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.deleteFilter"),
          type: 'button',
          meaning: '邮箱账户-删除黑白名单'
        }],
        onClick: this.handleDelete,
        disabled: selectedRows.length === 0,
        loading: deleteLoading
      }, intl.get('cpaas.common.status.delete').d('删除'))), /*#__PURE__*/React.createElement(EditTable, {
        bordered: true,
        loading: fetchLoading,
        rowSelection: rowSelection,
        rowKey: "emailFilterId",
        columns: columns,
        dataSource: dataSource,
        pagination: pagination,
        onChange: this.handlePagination
      }));
    }
  }]);

  return AppSourceForm;
}(React.PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "handleOk", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSelectChange", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSelectChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDelete", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDelete"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCreate", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCreate"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handlePagination", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePagination"), _class2.prototype)), _class2)) || _class);
export { AppSourceForm as default };